package com.jiai.cloud.api;


import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.tools.RestResultGenerator;
import com.jiai.cloud.entity.TApplication;
import com.jiai.cloud.feign.ApplicationAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with j360-cloud-all -> me.j360.cloud.eurekaclient.api.
 * User: min_xu
 * Date: 2015/10/9
 * Time: 10:53
 * 说明：
 */

@SuppressWarnings("Duplicates")
@RestController
@RequestMapping("/api/v1/application")
public class APIApplicationController {


    @Autowired
    ApplicationAPI applicationAPI;


    /**
     * 直接调用feign，feign会去调用eurekaService
     * */

    //增
    @RequestMapping(value = "/addApplication", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object saveApplication (@RequestBody TApplication  tApplication) throws ServiceException {
        try {
            applicationAPI.addApplication(tApplication);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("插入失敗", e.getMessage());
        }
    }

    //查询所有的
    @RequestMapping(value = "/findByAll/{page}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    public Object findGoodsByAll(@PathVariable("page") Integer page1) throws ServiceException {
        try{
            return RestResultGenerator.genSuccessResult(applicationAPI.selectPage(page1));
        }catch (Exception e){
            throw new ServiceException("chaxun",e.getMessage());
        }

    }
    //通过ID查询
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object findById(@PathVariable("id") Integer id) throws ServiceException {
        try{
            applicationAPI.selectById(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }


    //通过ID删除
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException {
        try{
            applicationAPI.deleteId(id);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }


    // 保存和更新
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit(@RequestBody TApplication tApplication)throws ServiceException{
        try {
            applicationAPI.insertOrUpdate(tApplication);

            return RestResultGenerator.genSuccessResult() ;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }

}
